import moment from 'moment'
export function dealy(time = 1000) {
  return new Promise(res => {
    setTimeout(res, time)
  })
}

export const delay = dealy

export function toLocaleDateString(time?: string | number | Date) {
  if (time) {
    return new Date(time).toLocaleDateString()
  }
  return ''
}

export function toLocaleString(time: string | number | Date) {
  if (time) {
    return new Date(time).toLocaleString()
  }
  return ''
}

export const MYDay = ['日', '一', '二', '三', '四', '五', '六']

export type ITimemyday = {
  D: number;
  M: number;
  W: string;
  Y: number;
  S: number;
  H: number;
}

export function Timemyday(): ITimemyday {
  const date = new Date
  return {
    D: date.getDate(),
    M: date.getMonth() + 1,
    W: MYDay[date.getDay()],
    Y: date.getFullYear(),
    S: date.getSeconds(),
    H: date.getHours(),

  }
}

/**
 * 
 * @param time 
 * @returns 返回 YYYY-MM-DD HH-MM
 */
export function TimeYH(time?: string | number | Date) {
  const data = new Date(time || new Date)
  return moment(data).format('YYYY-MM-DD HH:mm ')
}

/**
 * 
 * @param time 
 * @returns 返回 MM-DD HH-MM
 */
export function TimeYH1(time?: string | number | Date) {
  const data = new Date(time || new Date)
  return moment(data).format('MM-DD HH:mm ')
}

/**
 * 
 * @param time 
 * @returns 返回 YYYY-MM-DD
 */
export function TimeYH2(time?: string | number | Date) {
  const data = new Date(time || new Date)
  return moment(data).format('YYYY-MM-DD')
}

export function tomoment(str: string, type?: string, totype?: string) {
  return moment(str, type).format(totype)
}


export const shortcuts = [
  {
    text: "上周",
    value: () => {
      const start = moment().subtract(1, 'week').day(1).unix() * 1000;
      const end = moment(moment().day(0).format('YYYY-MM-DD 23:59:59')).unix() * 1000;
      return [start, end];
    },
  },
  {
    text: "本周",
    value: () => {
      if (moment().day() === 0) {
        const end = moment().day(0).unix() * 1000;
        const start = moment().subtract(1, 'week').day(1).unix() * 1000;
        return [start, end];
      } else {
        const end = moment().add(1, 'week').day(0).unix() * 1000;
        const start = moment().day(1).unix() * 1000;
        return [start, end];
      }

    },
  },
  {
    text: "本月",
    value: () => {
      const end = new Date();
      const start = moment().date(1).unix() * 1000;
      return [start, end];
    },
  },
  {
    text: "最近7天",
    value: () => {
      const end = new Date();
      const start = moment().subtract(7, 'day').unix() * 1000;
      return [start, end];
    },
  },
  {
    text: "最近30天",
    value: () => {
      const end = new Date();
      const start = moment().subtract(30, 'day').unix() * 1000;
      return [start, end];
    },
  },
  {
    text: "最近90天",
    value: () => {
      const end = new Date();
      const start = moment().subtract(90, 'day').unix() * 1000;
      return [start, end];
    },
  },
  {
    text: "近一年",
    value: () => {
      const end = Date.now();
      const start = moment().subtract(1, 'year').unix() * 1000;
      return [start, end];
    },
  },
  {
    text: "全部",
    value: () => {
      const end = new Date();
      const start = moment('1970', 'YYYY').unix() * 1000;
      return [start, end];
    },
  }
]

export const videoDimensions = function (video: HTMLVideoElement) {
  // Ratio of the video's intrisic dimensions
  var videoRatio = video.videoWidth / video.videoHeight;
  // The width and height of the video element
  var width = video.offsetWidth,
    height = video.offsetHeight;
  // The ratio of the element's width to its height
  var elementRatio = width / height;
  // If the video element is short and wide
  if (elementRatio > videoRatio) width = height * videoRatio;
  // It must be tall and thin, or exactly equal to the original ratio
  else height = width / videoRatio;
  return {
    width: width,
    height: height
  };
}